#include "CCmdInnerQQFriend.h"
#include "../mainctrl.h"
#include "../cmd/dataprepare.h"
#include "../cmd/cmdselectdatafromdbsvr.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_QQ_FRIEND, CCmdInnerQQFriend);

int CCmdInnerQQFriend::Do(void* pData)
{
    if (pData != NULL)
    {
        CT_WARNING(("inner cmd CCmdInnerQQFriend get data"));
        return Done(RET_OK);
    }

    if (pstMainCtrl == NULL)
    {
        CT_ERROR(("mainctrl point is null"));
        return Done(RET_SYSTEM_FAIL);
    }

    if (_uiGID == 0)
    {
        CT_WARNING((""));
        return Done(RET_OK);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdInnerQQFriend::CallBack(int iRet)
{
    CCmdSelectDataFromDBSvr* pstCmd = (CCmdSelectDataFromDBSvr*)m_pstChild;

    if (pstCmd->stAns.stResult.iResultID != RET_OK)
    {
        CT_TRACE(("QQFriend: no inviter openid(%s)!",_szInviterToken.Get()));
        return Done(RET_OK);
    }

    int iIndex = pstMainCtrl->m_stUserTokenToGID.HashFind(_szInviterToken);
    if (iIndex < 0)
    {
        CT_TRACE(("QQFriend: no inviter openid(%s)!",_szInviterToken.Get()));
        return Done(RET_OK);
    }

    return LogicDo(RET_OK);
}

int CCmdInnerQQFriend::LogicDo(int iRet)
{
    if (iRet != RET_OK)
    {
        return iRet;
    }

    int iIndex = 0;
    bool bNeedUpdate = false;

    PREPARE_PLAYER_DETAIL_EX1_INFO(this, _uiGID);
    ASSERT_PLAYER_DETAIL_EX1_INFO(_uiGID);

    iIndex = pstMainCtrl->m_stPlayerDetailEx1InfoHash.HashFind(_uiGID);
    if (iIndex < 0)
    {
        return Done(RET_OK);
    }
    CPlayerDetailInfoEx1& stPlayerDetailEx1 =
        pstMainCtrl->m_stPlayerDetailEx1InfoHash[iIndex];

	switch (_iSubCmd)
	{
    case CFriendMgr::CMD_QQ_FRIEND_SUB_PREPARE_TOKEN_GID:
        {
            iIndex = pstMainCtrl->m_stUserTokenToGID.HashFind(_szInviterToken);
            if (iIndex >= 0)
            {
                CGidList& stGidListTmp =
                    pstMainCtrl->m_stUserTokenToGID[iIndex];
                if (stGidListTmp._auiGID.Size() <= 0)
                {
                    CT_WARNING(("QQFriend: inviter openid(%s) zero gid!",
                        _szInviterToken.Get()));
                    return Done(RET_OK);
                }
            
                stPlayerDetailEx1._stQQFriend._uiInviterGID =
                    stGidListTmp._auiGID[0];

                pstMainCtrl->m_stFriendMgr.BeInvitedRoleTrigger(
                    stPlayerDetailEx1,
                    CFriendMgr::CMD_QQ_FRIEND_SUB_ADD_INVITE_NUM, 0);

                //此命令相对特殊：由被邀请者调用需要提前完成退出不往下执行
                CVarObjectKey stKey;
                stKey.m_chDataType = DT_PLAYER_DETAIL_EX1_INFO;
                stKey.m_u64ID = stPlayerDetailEx1._uiGID;
                CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerDetailEx1,
                    VOP_UPDATE);

                return Done(RET_OK);
            }
            else
            {
                CCmdSelectDataFromDBSvr* pstCmd =
                    (CCmdSelectDataFromDBSvr*)CT_CMDFACTORY->CreateCmd(
                        ID_MAINDB_SELECTDATA);
                if (!pstCmd)
                {
                    CT_WARNING(("create cmd error"));
                    return Done(RET_OK);
                }
            
                pstCmd->pstMainCtrl = pstMainCtrl;
                pstCmd->stReq.m_ucCheckUpdateMgr = 0;
                pstCmd->stReq.m_stKey.m_chDataType = DT_PLAYER_GID;
                pstCmd->stReq.m_stKey.m_sStrID.Set("%s", _szInviterToken());
            
                RegisterChild(pstCmd);
            
                return pstCmd->Do(NULL);
            }
        }
        break;
    case CFriendMgr::CMD_QQ_FRIEND_SUB_PREPARE_BE_INVITED:
        {
            iRet = pstMainCtrl->m_stFriendMgr.BeInvitedRoleTrigger(
                stPlayerDetailEx1, _iValue, _uiValue);
        }
        break;
    case CFriendMgr::CMD_QQ_FRIEND_SUB_ADD_INVITE_NUM:
        {
            iRet = pstMainCtrl->m_stFriendMgr.AddInviteNum(stPlayerDetailEx1,
                bNeedUpdate);
        }
        break;
    case CFriendMgr::CMD_QQ_FRIEND_SUB_ADD_PAY_ANY_NUM:
        {
            iRet = pstMainCtrl->m_stFriendMgr.AddPayAnyNum(stPlayerDetailEx1,
                _uiValue, bNeedUpdate);
        }
        break;
    case CFriendMgr::CMD_QQ_FRIEND_SUB_ADD_LVL_UP_NUM:
        {
            iRet = pstMainCtrl->m_stFriendMgr.AddLvlUpNum(stPlayerDetailEx1,
                bNeedUpdate);
        }
        break;
    case CFriendMgr::CMD_QQ_FRIEND_SUB_ADD_PAY_Q_COUNT:
        {
            iRet = pstMainCtrl->m_stFriendMgr.AddPayQCount(stPlayerDetailEx1,
                _uiValue, bNeedUpdate);
        }
        break;
    case CFriendMgr::CMD_QQ_FRIEND_SUB_DAILY_INVITE:
        {
            iRet = pstMainCtrl->m_stFriendMgr.InviteQQFriend(stPlayerDetailEx1,
                bNeedUpdate);
        }
        break;
	default:
		iRet = ERROR_MAIN_SUB_CMD;
		break;
	}

    if (iRet)
    {
        CT_TRACE(("user(gid=%u) deal qq friend op(sub=%d) error(%d)",
            _uiGID,_iSubCmd,iRet));
        return Done(RET_OK);
    }

    if (bNeedUpdate)
    {
        CVarObjectKey stKey;
        stKey.m_chDataType = DT_PLAYER_DETAIL_EX1_INFO;
        stKey.m_u64ID = stPlayerDetailEx1._uiGID;
        CMainCtrl::m_pstMain->UpdateData(stKey, stPlayerDetailEx1, VOP_UPDATE);
    }

    //如果角色在线 下发更新后的数据
    if (bNeedUpdate && pstMainCtrl->m_stPlayerOnline.CheckOnline(_uiGID))
    {
        MsgAnsQQFriend stAns;
        stAns.set_gid((int)_uiGID);
        stAns.set_req_type(CFriendMgr::CMD_QQ_FRIEND_SUB_GET_INFO);
        iRet = pstMainCtrl->m_stFriendMgr.GetQQFriendInfo(stPlayerDetailEx1,
            stAns);
        pstMainCtrl->NotifyOnlineUser(_uiGID, ID_MAIN_QQ_FRIEND, stAns, false);
    }

    return Done(RET_OK);
}

